<template>
  <el-dialog
    title="审核"
    :visible.sync="visible"
    width="660px"
    append-to-body
  >
    <avue-form
      ref="addForm"
      v-model="form"
      :option="auditReasonOption"
    >
      <template #releaseDate>
        <el-date-picker
          v-model="form.releaseDate"
          align="right"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
        <div>
          可以预审核通过，然后到对应日期每天10点才会展示在用户的日记列表中
        </div>
      </template>
    </avue-form>
    <template #footer>
      <el-button
        type="primary"
        :loading="loading"
        @click="submit"
      >
        确定
      </el-button>
      <el-button @click="visible = false">取消</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { auditReasonOption } from '@/const/crud/pet/adoptDiary';
import { ADOPT_DIARY_AUDIT_STATUS } from '@/const/type';
import { auditAdoptDiary } from '@/api/core/adopt';
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
    },
  },
  data() {
    return {
      ADOPT_DIARY_AUDIT_STATUS,
      auditReasonOption,
      form: {
        status: ADOPT_DIARY_AUDIT_STATUS.PASSED,
        releaseDate: '',
      },
      loading: false,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        },
      },
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = {
          status: ADOPT_DIARY_AUDIT_STATUS.PASSED,
          releaseType: '0',
        };
      } else {
        this.$refs.addForm.resetFields();
      }
    },
  },
  methods: {
    submit() {
      this.$refs.addForm.validate((valid, done) => {
        if (valid) {
          this.form.id = this.id;
          auditAdoptDiary(this.form)
            .then((res) => {
              this.$emit('onSuccess');
              this.visible = false;
              this.loading = false;
              this.$message.success('审核成功！');
              done();
            })
            .catch((err) => {
              this.loading = false;
              done();
              throw new Error(err);
            });
        }
      });
    },
  },
};
</script>
